package com.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.pojo.Score;
import com.service.ScoreService;
import com.service.StudentService;

@Controller
@RequestMapping("/student")
public class StudentController {
	@Resource
	private StudentService studentService;
	@Resource
	private ScoreService scoreService;
	
	
	
	@RequestMapping("/result")
	public String loginAction(HttpServletRequest request, Model model) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		// 从index.jsp的表单获取到的用户输入的姓名 xm
		String studentName = request.getParameter("studentName");
		// //从index.jsp的表单获取到的用户输入的学号 xh
		String studentNumber = request.getParameter("studentNumber");
		// 从数据库查询到的用户密码
		String xm = this.studentService.getStudentNameById(studentNumber);
		String message = "";
		// 判断姓名是否一致
		if (studentName.equals(xm)) {
			message = "恭喜你，登陆成功！";
			System.out.println("message:"+message);
			model.addAttribute("message", message);
			model.addAttribute("studentName", xm);
			model.addAttribute("studentNumber", studentNumber);
			//根据学号查询成绩
			List<Score> scores = scoreService.getScoreByStudentNumber(studentNumber);
			List<Map<String, String>> list = new ArrayList<Map<String, String>>();
			for(Score score:scores) {
				//将2008-06-10 10:13:30.110  转换成 2008年6月
				String date=score.getQdcjsj().substring(0, 7);
				if (date.contains("-0")) {
					score.setQdcjsj(date.replace("-0", "年")+"月");
				}else
					score.setQdcjsj(date.replace("-", "年")+"月");
			
				//处理是否及格  true为及格 false不及格
				if (score.getZhcj()==null||"".equals(score.getZhcj())) {
					score.setPass(false);
				}else if (Integer.valueOf(score.getZhcj())>=60) {
					score.setPass(true);
					System.out.println(score.isPass()+"--"+score.getZhcj());
				}else {
					score.setPass(false);
				}
				
				
				Map<String, String> map= BeanUtils.describe(score);
				list.add(map);
			}
			model.addAttribute("score", list);
			return "query";
		} else {
			model.addAttribute("message", message);
			message = "您输入的学号、姓名有误，请重新输入！";
			model.addAttribute("message", message);
			//这块儿我觉得我写的不对。。。
			return "error";
		}
		
		

	}

	
	@RequestMapping("/select")
	public String queryAction(HttpServletRequest request, Model model) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		// //从index.jsp的表单获取到的用户输入的学号 xh
		String studentNumber = request.getParameter("studentNumber");
		String studentName = this.studentService.getStudentNameById(studentNumber);
			//根据学号查询成绩
			List<Score> scores = scoreService.getScoreByStudentNumber2(studentNumber);
			List<Map<String, String>> list = new ArrayList<Map<String, String>>();
			for(Score score:scores) {
				//将2008-06-10 10:13:30.110  转换成 2008年6月
				String date=score.getQdcjsj().substring(0, 7);
				if (date.contains("-0")) {
					score.setQdcjsj(date.replace("-0", "年")+"月");
				}else
					score.setQdcjsj(date.replace("-", "年")+"月");
			
				//处理是否及格  true为及格 false不及格
				if (score.getZhcj()==null||"".equals(score.getZhcj())) {
					score.setPass(false);
				}else if (Integer.valueOf(score.getZhcj())>=60) {
					score.setPass(true);
				}else {
					score.setPass(false);
				}
				
				
				Map<String, String> map= BeanUtils.describe(score);
				list.add(map);
			}
			model.addAttribute("score", list);
			model.addAttribute("studentNumber", studentNumber);
			model.addAttribute("studentName", studentName);
			return "query2";
		
		

	}

}
